package com.hundsun.cprs.yyt.gateway.service.trade.impl;

import com.hundsun.cprs.yyt.common.enums.EnumBusinessCode;
import com.hundsun.cprs.yyt.common.enums.EnumCommonCode;
import com.hundsun.cprs.yyt.common.exception.ServiceCodeException;
import com.hundsun.cprs.yyt.gateway.request.QuotaExchangeRequest;
import com.hundsun.cprs.yyt.gateway.request.QuotaHoldRequest;
import com.hundsun.cprs.yyt.gateway.request.QuotaRequest;
import com.hundsun.cprs.yyt.gateway.response.*;
import com.hundsun.cprs.yyt.gateway.service.BaseService;
import com.hundsun.cprs.yyt.gateway.service.trade.QuotaCommonService;
import com.hundsun.cprs.yyt.gateway.vo.QuotaExchangeRecordVo;
import com.hundsun.cprs.yyt.gateway.vo.QuotaHoldVo;
import com.hundsun.cprs.yyt.gateway.vo.QuotaVo;
import com.hundsun.cprs.yyt.service.quota.domain.Quota;
import com.hundsun.cprs.yyt.service.quota.domain.query.QuotaExchangeRecordQuery;
import com.hundsun.cprs.yyt.service.quota.domain.query.QuotaHoldQuery;
import com.hundsun.cprs.yyt.service.quota.domain.query.QuotaQuery;
import com.hundsun.cprs.yyt.service.quota.dto.QuotaExchangeRecordInfo;
import com.hundsun.cprs.yyt.service.quota.enums.EnumQuotaType;
import com.hundsun.cprs.yyt.service.quota.service.QuotaExchangeRecordService;
import com.hundsun.cprs.yyt.service.quota.service.QuotaHoldService;
import com.hundsun.cprs.yyt.service.quota.service.QuotaService;
import com.hundsun.cprs.yyt.util.BeanUtils;
import com.hundsun.cprs.yyt.util.DateUtil;
import com.hundsun.cprs.yyt.util.StringUtil;
import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 配额相关对外T2接口实现类
 * @author herong22384
 * @date 2019/8/9 17:09
 */
@Service
public class QuotaCommonServiceImpl extends BaseService implements QuotaCommonService {

    @Autowired
    private QuotaService quotaService;
    @Autowired
    private QuotaHoldService quotaHoldService;
    @Autowired
    private QuotaExchangeRecordService recordService;

    @Override
    public QuotaHoldListRResponse getMyQuotaHoldList(QuotaHoldRequest request) {
        QuotaHoldListRResponse response = new QuotaHoldListRResponse();
        try {
            Validate.notNull(request.getUserId(),"用户信息不能为空");
            QuotaHoldQuery query = BeanUtils.transfrom(QuotaHoldQuery.class,request);
            quotaHoldService.queryByPage(query);
            if (query.getTotalCount() > 0){
                response.setTotalCount(query.getTotalCount());
                response.setQuotaHoldVos(BeanUtils.batchTransform(QuotaHoldVo.class,query.getData()));
            }
        } catch (IllegalArgumentException e) {
            response.setErrorNo(EnumBusinessCode.BUSINESS_1000.getErrorNo());
            response.setErrorInfo(e.getMessage());
        } catch (ServiceCodeException e) {
            response.setErrorNo(e.getErrorCode());
            response.setErrorInfo(e.getMessage());
        } catch (Exception e) {
            logger.error("处理异常", e);
            response.setErrorNo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorNo());
            response.setErrorInfo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorInfo());
        }
        return response;
    }

    @Override
    public QuotaListResponse getQuotaList(QuotaRequest request) {
        QuotaListResponse response = new QuotaListResponse();
        try {
            QuotaQuery query = BeanUtils.transfrom(QuotaQuery.class,request);
            query.setType(request.getQuotaType());
            quotaService.queryByPage(query);
            if (query.getTotalCount() > 0){
                response.setTotalCount(query.getTotalCount());
                response.setQuotaVos(BeanUtils.batchTransform(QuotaVo.class,query.getData()));
            }
        } catch (IllegalArgumentException e) {
            response.setErrorNo(EnumBusinessCode.BUSINESS_1000.getErrorNo());
            response.setErrorInfo(e.getMessage());
        } catch (ServiceCodeException e) {
            response.setErrorNo(e.getErrorCode());
            response.setErrorInfo(e.getMessage());
        } catch (Exception e) {
            logger.error("处理异常", e);
            response.setErrorNo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorNo());
            response.setErrorInfo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorInfo());
        }
        return response;
    }

    @Override
    public BaseResponse quotaExchange(QuotaExchangeRequest request) {
        BaseResponse response = new BaseResponse();
        try {
            Validate.notNull(request.getUserId(),"用户信息不能为空");
            Validate.notNull(request.getSourceQuotaId(),"源配额类型不能为空");
            Validate.notNull(request.getAimQuotaId(),"目标配额类型不能为空");
            //获取目标专用配额类型的规则
            Quota quota = quotaService.selectQuoteById(request.getAimQuotaId());
            if (quota == null){
                throw new ServiceCodeException(EnumBusinessCode.BUSINESS_3233.getErrorNo(),EnumBusinessCode.BUSINESS_3233.getErrorInfo());
            }
            recordService.exchange(request.getUserId(),request.getSourceQuotaId(),request.getAimQuotaId(),quota.getExchangeAmount());

        } catch (IllegalArgumentException e) {
            response.setErrorNo(EnumBusinessCode.BUSINESS_1000.getErrorNo());
            response.setErrorInfo(e.getMessage());
        } catch (ServiceCodeException e) {
            response.setErrorNo(e.getErrorCode());
            response.setErrorInfo(e.getMessage());
        } catch (Exception e) {
            logger.error("处理异常", e);
            response.setErrorNo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorNo());
            response.setErrorInfo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorInfo());
        }
        return response;
    }

    @Override
    public QuotaExchangeListResponse getQuotaRecordList(QuotaExchangeRequest request) {
        QuotaExchangeListResponse response = new QuotaExchangeListResponse();
        try {
            QuotaExchangeRecordQuery query = BeanUtils.transfrom(QuotaExchangeRecordQuery.class,request);
            if(request.getStartDate() != null) {
                query.setStartDate(DateUtil.strToDate(request.getStartDate(),"yyyy-MM-dd HH:mm:ss"));
            }
            if(request.getEndDate() != null) {
                query.setEndDate(DateUtil.strToDate(request.getEndDate(),"yyyy-MM-dd HH:mm:ss"));
            }
            recordService.queryByPage(query);
            if (query.getTotalCount() > 0){
                response.setTotalCount(query.getTotalCount());
                response.setQuotaExchangeRecordVos(BeanUtils.batchTransform(QuotaExchangeRecordVo.class,query.getData()));
            }
        } catch (IllegalArgumentException e) {
            response.setErrorNo(EnumBusinessCode.BUSINESS_1000.getErrorNo());
            response.setErrorInfo(e.getMessage());
        } catch (ServiceCodeException e) {
            response.setErrorNo(e.getErrorCode());
            response.setErrorInfo(e.getMessage());
        } catch (Exception e) {
            logger.error("处理异常", e);
            response.setErrorNo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorNo());
            response.setErrorInfo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorInfo());
        }
        return response;
    }

    @Override
    public QuotaExchangeResponse getQuotaRecordDetail(Long userId, Long recordId) {
        QuotaExchangeResponse response = new QuotaExchangeResponse();
        try {
            Validate.notNull(userId,"用户信息不能为空");
            Validate.notNull(recordId,"兑换记录id不能为空");
            QuotaExchangeRecordInfo recordInfo = recordService.selectByRecordId(recordId);
            if (recordInfo.getUserId().compareTo(userId)!=0){
                throw new ServiceCodeException(EnumBusinessCode.BUSINESS_3237.getErrorNo(),EnumBusinessCode.BUSINESS_3237.getErrorInfo());
            }
            if (recordInfo != null){
                response.setQuotaExchangeRecordVo(BeanUtils.transfrom(QuotaExchangeRecordVo.class,recordInfo));
            }
        } catch (IllegalArgumentException e) {
            response.setErrorNo(EnumBusinessCode.BUSINESS_1000.getErrorNo());
            response.setErrorInfo(e.getMessage());
        } catch (ServiceCodeException e) {
            response.setErrorNo(e.getErrorCode());
            response.setErrorInfo(e.getMessage());
        } catch (Exception e) {
            logger.error("处理异常", e);
            response.setErrorNo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorNo());
            response.setErrorInfo(EnumCommonCode.COMMON_SERVER_ERROR.getErrorInfo());
        }
        return response;
    }
}
